package com.imau.controller;

import com.alibaba.fastjson.JSON;
import com.imau.bean.User;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/JSONServlet")
public class JSONServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        //接收前端传递的JSON
        BufferedReader reader = request.getReader();
        String data = reader.readLine();

        //.....JSON和java对象之间转换
        System.out.println("data==="+data);
        //反射 泛型

        User user = JSON.parseObject(data, User.class);


        System.out.println(user);
        response.getWriter().write(JSON.toJSONString(user));
    }
}
